#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time   : 2022/6/2 22:43 
# @Author : 郭林莉


#!/usr/bin/python3

import xlrd
from config.settings import ConfigHandler
from ruamel.yaml import YAML
import os

yaml = YAML()


def read_xls(filename):

    # 打开Excel文件
    data = xlrd.open_workbook(filename)
    # 读取第一个工作表和表名
    table = data.sheets()[0]
    tablesheet = data.sheet_names()[0]
    # 统计行数
    rows = table.nrows
    data = {}  # 存放数据
    ymal_dirmctory = ConfigHandler.data_path + f'{tablesheet}'

    for v in range(1, rows):
        values = table.row_values(v)
        excel_yaml_path = str(values[10])
        print("excel_yaml_path",excel_yaml_path)
        yaml_path = ymal_dirmctory+excel_yaml_path[:-2]+'.yaml'
        # print('ymal_dirmctory', ymal_dirmctory)
        # print('yaml_path', yaml_path)
        if not os.path.exists(ymal_dirmctory):
            os.mkdir(ymal_dirmctory)
        if str(values[0]) == '同上':
            pass
        else:
            data["case_common"]=eval(values[0])
        case_name = excel_yaml_path[1:]
        print('名字',case_name)
        yamlassert = eval(values[11])
        if values[7] == '':
            data[case_name]={
                        "host": str(values[1]),
                        "url": str(values[2]),  # 这里我只需要字符型数据，加了str(),根据实际自己取舍
                        "method": str(values[3]),
                        "detail": str(values[4]),
                        "headers": eval(values[5]),
                        "requestType": str(values[6]),
                        "is_run": True,
                        "data": eval(values[8]),
                        "assert": yamlassert,
                        'sql': ""
                        }

        with open(yaml_path, mode='w', encoding='utf-8') as file:
            yaml.dump(data, file)
    return data



dmdata = read_xls(ConfigHandler.file_path + r'\stp.xlsx')
print(dmdata)
# print(jsonpath.jsonpath(dddddd,"$..yamlpath"))

